package cn.iocoder.yudao.adminserver.modules.fi.controller.coreTask;

import cn.iocoder.yudao.adminserver.modules.fi.controller.coreTask.vo.*;
import cn.iocoder.yudao.adminserver.modules.fi.controller.project.vo.ProjectRespVO;
import cn.iocoder.yudao.adminserver.modules.fi.convert.coreTask.CoreTaskConvert;
import cn.iocoder.yudao.adminserver.modules.fi.convert.project.ProjectConvert;
import cn.iocoder.yudao.adminserver.modules.fi.dal.dataobject.coreTask.CoreTaskDO;
import cn.iocoder.yudao.adminserver.modules.fi.dal.dataobject.project.ProjectDO;
import cn.iocoder.yudao.adminserver.modules.fi.service.coreTask.CoreTaskService;
import cn.iocoder.yudao.adminserver.modules.fi.service.project.ProjectService;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "项目反馈")
@RestController
@RequestMapping("/fi/coreTask")
@Validated
public class CoreTaskController {

    @Resource
    private CoreTaskService Service;

    @Resource
    private SysUserService userService;

    @Resource
    private ProjectService projectService;

    @PostMapping("/create")
    @ApiOperation("创建项目反馈")
    @PreAuthorize("@ss.hasPermission('fi:coreTask:create')")
    public CommonResult<Long> create(@Valid @RequestBody CoreTaskCreateReqVO createReqVO) {
        return success(Service.create(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新项目反馈")
    @PreAuthorize("@ss.hasPermission('fi:coreTask:edit')")
    public CommonResult<Boolean> update(@Valid @RequestBody CoreTaskUpdateReqVO updateReqVO) {
        Service.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除项目反馈")
    @ApiImplicitParam(name = "id", value = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fi:coreTask:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        Service.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得项目反馈")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('fi:coreTask:query')")
    public CommonResult<CoreTaskRespVO> get(@RequestParam("id") Long id) {
        CoreTaskDO obj = Service.get(id);
        Collection<Long> userIds = new ArrayList<>();
        CollectionUtils.addIfNotNull(userIds, obj.getDoTaskUid());
        CollectionUtils.addIfNotNull(userIds, Long.parseLong(obj.getCreator()));
        Map<Long, SysUserDO> userMap = this.userService.getUserMap(userIds);
        return success(CoreTaskConvert.INSTANCE.convert(obj, userMap, null));
    }

    @GetMapping("/list")
    @ApiOperation("获得项目反馈列表")
    @PreAuthorize("@ss.hasPermission('fi:coreTask:query')")
    public CommonResult<List<CoreTaskRespVO>> getList(@Valid CoreTaskExportReqVO reqVO) {
        List<CoreTaskDO> list = Service.getList(reqVO);
        Collection userIds = CollectionUtils.convertList(list, CoreTaskDO::getDoTaskUid);
        Map<Long, SysUserDO> userMap = this.userService.getUserMap(userIds);

        return success(CoreTaskConvert.INSTANCE.convertList(list, userMap));
    }

    @GetMapping("/page")
    @ApiOperation("获得项目反馈分页")
    @PreAuthorize("@ss.hasPermission('fi:coreTask:query')")
    public CommonResult<PageResult<CoreTaskRespVO>> getPage(@Valid CoreTaskPageReqVO pageVO) {
        PageResult<CoreTaskDO> pageResult = Service.getPage(pageVO);
        Collection<Long> projectIds = CollectionUtils.convertList(pageResult.getList(), CoreTaskDO::getProjectId);
        Map<Long, ProjectDO> projectMap = this.projectService.getProjectMaps(projectIds);
        Collection<Long> userIds = CollectionUtils.convertList(pageResult.getList(), CoreTaskDO::getDoTaskUid);
        Map<Long, SysUserDO> userMap = this.userService.getUserMap(userIds);
        List<CoreTaskRespVO> list = new ArrayList<>();
        pageResult.getList().forEach(coreTask -> {
            CoreTaskRespVO respVO = CoreTaskConvert.INSTANCE.convert(coreTask, userMap, projectMap);
            list.add(respVO);
        });
        return success(new PageResult<CoreTaskRespVO>(list, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出项目反馈 Excel")
    @PreAuthorize("@ss.hasPermission('fi:coreTask:export')")
    @OperateLog(type = EXPORT)
    public void exportExcel(@Valid CoreTaskExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CoreTaskDO> list = Service.getList(exportReqVO);
        // 导出 Excel
        List<CoreTaskExcelVO> datas = CoreTaskConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目反馈.xls", "数据", CoreTaskExcelVO.class, datas);
    }

}
